package com.roger.cmdkit;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;

public class AutoFlash extends JFrame {
    private JFrame jf = new JFrame();
    private String rootPath = System.getProperty("user.dir");

    private JComboBox<String> jcb1;
    private JComboBox<String> jcb2;
    private JComboBox<String> jcb3;
    private JComboBox<String> jcb4;
    private JTextArea jta;

    public static void main(String[] arg){
        new AutoFlash().createGui();
    }

    private void createGui(){
        JPanel jp1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel jp2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel jp3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel jp4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel jp5 = new JPanel();


        JLabel jlb1 = new JLabel("1.Whether to only download version file:");
        jlb1.setFont(new Font("Times New Roman",Font.PLAIN, 20));
        JLabel jlb2 = new JLabel("2.Choose project:");
        jlb2.setFont(new Font("Times New Roman",Font.PLAIN, 20));
        JLabel jlb3 = new JLabel("3.Choose release type:");
        jlb3.setFont(new Font("Times New Roman",Font.PLAIN, 20));
        JLabel jlb4 = new JLabel("4.Choose sku:");
        jlb4.setFont(new Font("Times New Roman",Font.PLAIN, 20));




        String[] jg1 = { "True", "False"};
        String[] jg2 = { "Achillies6", "Amar"};
        String[] jg3 = { "WeeklyRelease", "Candidate"};
        String[] jg4 = { "row_wifi", "row_data","row_data_alexa","row_call_mp"};

        jcb1 = new JComboBox<>(jg1);
        jcb2 = new JComboBox<>(jg2);
        jcb3 = new JComboBox<>(jg3);
        jcb4 = new JComboBox<>(jg4);

        jp1.add(jlb1);
        jp1.add(jcb1);
        jp2.add(jlb2);
        jp2.add(jcb2);
        jp3.add(jlb3);
        jp3.add(jcb3);
        jp4.add(jlb4);
        jp4.add(jcb4);


        JButton jb = new JButton("start");
        jp5.add(jb);

        jf.add(jp1);
        jf.add(jp2);
        jf.add(jp3);
        jf.add(jp4);
        jf.add(jp5);

        jta = new JTextArea();
        jta.setFont(new Font("Times New Roman",Font.PLAIN, 20));
        jta.setLineWrap(true);
        jta.setPreferredSize(new Dimension(500,400));
        JScrollPane jsp = new JScrollPane(jta);
        jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jf.add(jsp);

        jf.setLayout(new GridLayout(6, 1));
        jf.setTitle("AutoFlash");
        jf.setSize(600, 400);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);

        jb.addActionListener(new start(this));

    }

    class start implements ActionListener {
        AutoFlash af;

        start(AutoFlash af){
            this.af=af;
        }

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println(af.jcb1.getSelectedItem());
            System.out.println(af.jcb2.getSelectedItem());
            System.out.println(af.jcb3.getSelectedItem());
            System.out.println(af.jcb4.getSelectedItem());
            StringBuilder s = new StringBuilder();
            s.append("====Start====\n");
            jta.setText(s.toString());
            String fName = null;
            if("True".equals(af.jcb1.getSelectedItem())){
                try {
                    File fd= new File("C:\\Users\\admin\\Desktop\\wtk\\rrr\\Download");
                    if(fd.listFiles().length > 0){
                        for(File f1 : fd.listFiles()){
                            if(f1.isFile()) {
                                 fName = f1.getName();
                            }
                        }

                        File fv = new File(rootPath+"\\Version\\");
                        if(fv.exists()) delFolder(fv);
                        File fv1 = new File(rootPath+"\\Version\\"+fName.substring(0, fName.lastIndexOf(".")));
                        fv1.mkdirs();

                    }
                    s.append("unzip file now\n");
                    jta.setText(s.toString());
                    JFileChooser fileChooser = new JFileChooser();
                    int i = fileChooser.showOpenDialog(getContentPane());
                    if (i == JFileChooser.APPROVE_OPTION) {
                        File selectedFile = fileChooser.getSelectedFile();// 获得选中的文件对象
                        jta.setText(selectedFile.getName());// 显示选中文件的名称
                    }
                     //Desktop.getDesktop().open(new File(rootPath + "\\SP_Flash_TooL\\flash_tool.exe"));
                    //Runtime.getRuntime().exec("cmd /c start " + rootPath + "\\7z.exe x Download\\"+fName+" -oVersion\\"+fName.substring(0, fName.lastIndexOf("."))+" -aoa");
                    //Runtime.getRuntime().exec("cmd /c start " + rootPath + "\\SP_Flash_TooL\\flash_tool.exe");
                } catch (Throwable e) {
                    e.printStackTrace();
                }
            }
        }

        private void delFolder(File folder) {
            if (folder.isDirectory()) {
                String[] files = folder.list();
                for (String file : files) {
                    delFolder(new File(folder, file));
                }
            }
            folder.delete();
        }

    }

}
